package cn.hh.wechatkit.pojo.card;
import cn.hh.wechatkit.util.WxCardSignUtil;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.RandomUtils;

import java.util.Date;

/**
 * Created this one by HMH on 2017/8/7.
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Wx_CardExt {

    String timestamp;
    String signature;
    String nonce_str;
    String code="";
    String openid="";

    public Wx_CardExt(String timestamp, String signature,String nonce_str) {
        this.timestamp = timestamp;
        this.signature = signature;
        this.nonce_str = nonce_str;
    }

    public static Wx_CardExt generateCardExt(String apiTicket,String cardId) {
        WxCardSignUtil wxCardSign = new WxCardSignUtil();
        String timestamp = String.valueOf(new Date().getTime()/1000);
        String nonce_str = String.valueOf(RandomUtils.nextLong(1,500000000L));
        wxCardSign.AddData(timestamp);
        wxCardSign.AddData(nonce_str);
        wxCardSign.AddData(apiTicket);
        wxCardSign.AddData(cardId);
        String signature = wxCardSign.GetSignature();
        return new Wx_CardExt(timestamp, signature, nonce_str);
    }

    @Override
    @JSONField(serialize = false)
    public String toString() {
        return "Wx_CardExt{" +
                "timestamp='" + timestamp + '\'' +
                ", signature='" + signature + '\'' +
                ", nonce_str='" + nonce_str + '\'' +
                ", code='" + code + '\'' +
                ", openid='" + openid + '\'' +
                '}';
    }
}
